//
//  MLWLTVideoPlayerViewController.m
//  KITCHEN_STORIES
//
//  Created by 孟令通 on 16/6/15.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWLTVideoPlayerViewController.h"

#import "MLWLTPlayerView.h"

@interface MLWLTVideoPlayerViewController ()<AVPlayerViewControllerDelegate, MLWPlayerDelegate>

@property (nonatomic, retain) AVPlayerViewController *playerVC;

@property (nonatomic, retain) AVPlayer *player;

@property (nonatomic, retain) AVPlayerItem *item;

@property (nonatomic, retain) AVPlayerLayer *layer;

@property (nonatomic, retain) UISlider *progressSlider;

@property (nonatomic, retain) MLWLTPlayerView *playerView;

@end

@implementation MLWLTVideoPlayerViewController

- (void)dealloc
{
    [_playerVC release];
    [_player release];
    [_item release];
    [_layer release];
    [_progressSlider release];
    [_playerView release];
    
    [super dealloc];
}

+(MLWLTVideoPlayerViewController *)sharePlayerInstance
{
    static MLWLTVideoPlayerViewController *playerVC = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        playerVC = [[MLWLTVideoPlayerViewController alloc] init];
        
    });
    
    return playerVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    
    
}

-(void)setUrl:(NSString *)url
{
    if (_url != url) {
        
        [_url release];
        
        _url = [url retain];
        
        self.playerView = [[MLWLTPlayerView alloc] initWithFrame:self.view.bounds];
        
        //    self.playerView.url = @"http://baobab.wdjcdn.com/1461897495660000111.mp4";
        
        self.playerView.url = _url;
        
        self.playerView.MLWDelegate = self;
        
        self.playerView.playerLayerGravity = MLWPlayerLayerGravityResizeAspect;
        
        [self.view addSubview:_playerView];
        [_playerView release];
        
    }
}

-(void)playerGoBackBtn
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark 使用 AVFoundation 创建播放器
- (void) createVideoPlayerByAVFoundation
{
    
    self.item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://baobab.wdjcdn.com/1461897495660000111.mp4"]];
    
    self.player = [[AVPlayer alloc] initWithPlayerItem:_item];
    
    // AVPlayerLayer 负责视频画面显示
    self.layer = [AVPlayerLayer playerLayerWithPlayer:_player];
    
    self.layer.frame = self.view.bounds;
    
    // 让视频根据frame居中
    self.layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    
    [self.view.layer addSublayer:_layer];
    
    // 设置横向全屏
    self.layer.bounds = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
    
    // 让视图旋转90°
    self.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1);
    
    [self createPlayerControl];
    
    [self.player play];
    
    
}

- (void)createPlayerControl
{
    
    UIView *progress = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, ScreenHeight)];
    
    progress.backgroundColor = [UIColor colorWithRed:0.04 green:0.04 blue:0.04 alpha:0.40];
    
    // 添加进度条
    self.progressSlider = [[UISlider alloc] initWithFrame:CGRectMake(50, 0, 4, ScreenHeight)];
    
    [self.view addSubview:_progressSlider];
    [_progressSlider release];
    
    [self.view addSubview:progress];
    [progress release];
    
}


#pragma mark 使用 AVPlayerViewController 创建播放器
- (void) createVideoPlayerByAVPlayerViewController
{
    
    // 创建音频播放对象
    AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://baobab.wdjcdn.com/1461897495660000111.mp4"]];
    
    self.playerVC = [[AVPlayerViewController alloc] init];
    
    self.playerVC.player = player;
    
    self.playerVC.view.frame = self.view.bounds;
    
    // 播放控件是否显示
    self.playerVC.showsPlaybackControls = YES;
    
    self.playerVC.delegate = self;
    
    [self.view addSubview:self.playerVC.view];
    
    player.externalPlaybackVideoGravity = AVLayerVideoGravityResizeAspectFill;
    
    [player play];
    
}


#pragma mark <AVPlayerViewControllerDelegate>
-(void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController
{
    NSLog(@"啥");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
